/**
 * 基础菜单 商品管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品入库</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索">
        <el-input size="small" v-model="formInline.receipt_No" placeholder="输入入库单号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="success" round  @click="search">搜索</el-button>
         <el-button type="primary"  @click="handleEdit()" round>新增入库</el-button>
      </el-form-item>
       <el-form-item>
        <el-upload class="upload-demo" action="/api/productionwarehouse/import" 
        :headers="headers" :on-success="handlesuccess" :before-upload="beforeUpload">
          <el-button type="warning"  round>导入入库单</el-button>
        </el-upload>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column align="center" prop="receipt_No" label="入库单号" width="160">
      </el-table-column>
      <el-table-column align="center" prop="material_code" label="产品编码" width="160">
      </el-table-column>
      <el-table-column align="center" prop="product_name" label="产品名称" width="160">
      </el-table-column>
      <el-table-column align="center" prop="product_classification" label="产品分类" width="160">
      </el-table-column>
      <el-table-column align="center" prop="product_model" label="产品型号" width="160">
      </el-table-column>
      <el-table-column align="center" prop="product_Brand" label="产品品牌" width="160">
      </el-table-column>
      <el-table-column align="center" prop="product_type" label="产品类型" width="160">
      </el-table-column>
      <el-table-column align="center" prop="receipt_quantity" label="入库数量" width="160">
      </el-table-column>
      <el-table-column align="center" prop="warehousename" label="入库仓库" width="160">
      </el-table-column>
      <el-table-column align="center" prop="warehousing_personnel" label="入库人员" width="160">
      </el-table-column>
       <el-table-column align="center" prop="warehousing_time" label="入库时间" width="160">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="160">
        <template slot-scope="scope">
          <el-button size="mini" disabled @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
        <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="入库单号" prop="receipt_No">
          <el-input size="small" v-model="editForm.receipt_No" auto-complete="off" :disabled="true" placeholder="请输入入库单号"></el-input>
        </el-form-item>
        <el-form-item label="产品编码" prop="material_code">
          <el-select v-model="editForm.material_code" @focus="addmaterial_code">
            <el-option v-for="item in material_codeData" :key="item.material_code" :value="item.material_code" :label="item.material_code"/>
          </el-select>
        </el-form-item>
        <el-form-item label="入库仓库" prop="warehousename">
          <el-select v-model="editForm.warehousename">
            <el-option v-for="item in warehouseData" :key="item.id" :value="item.warehousename" :label="item.warehousename"/>
          </el-select>
        </el-form-item>
        <el-form-item label="入库数量" prop="receipt_quantity">
          <el-input size="small" v-model="editForm.receipt_quantity" auto-complete="off" placeholder="请输入库数量"></el-input>
        </el-form-item>
        <el-form-item label="入库人员" prop="warehousing_personnel">
          <el-input size="small" v-model="editForm.warehousing_personnel" auto-complete="off" placeholder="请输入入库人姓名"></el-input>
        </el-form-item>
        <el-form-item label="入库时间" prop="warehousing_time">
          <el-date-picker
            v-model=" editForm.warehousing_time"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间">
    </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
     <!-- 显示仓库表 -->
    <el-dialog :title="title" :visible.sync="material_codeVisible" width="25%"  @click="closeDialog">
      <el-table
    :data="material_codedata"
    height="250"
    border
    style="width: 100%"
    highlight-current-row @current-change="handleCurrentChange">
    <el-table-column
      prop="material_code"
      label="产品编码"
      width="180">
    </el-table-column>
    <el-table-column
      prop="warehousename"
      label="仓库名称"
      width="160">
    </el-table-column>
  </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closematerial_code">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click=" submitmaterial_coder()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import{v4} from 'uuid'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      material_codeVisible:false,
      title: '添加',
      editForm: {
       receipt_No:"",
       material_code:"",
       warehousename:"",
       receipt_quantity:"",
       warehousing_personnel:"",
       warehousing_time:"",



        token: localStorage.getItem('logintoken')
      },
      // rules表单验证
      rules: {
        material_code: [
          { required: true, message: '请选择产品编码', trigger: 'blur' }
        ],
        warehousename: [{ required: true, message: '请输入部门代码', trigger: 'blur' }],
        receipt_quantity: [{ required: true, message: '请输入入库数量', trigger: 'blur' }],
        warehousing_personnel: [{ required: true, message: '请输入入库人员', trigger: 'blur' }],
        warehousing_time: [{ required: true, message: '请选择入库时间', trigger: 'blur' }],
      },
      
      formInline: {
        receipt_No:"",
        page: 1,
        limit: 10,
        varLable: '',
        varName: '',
        token: localStorage.getItem('logintoken')
      },
      // 删除部门
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      warehouseData: [], //仓库数据
      material_codeData:[],//编码数据
      material_codedata:[],//仓库信息
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
  },
   // 获取token
  computed: {
    headers() {
      return{
        "Authorization": localStorage.getItem('logintoken')
      }
    }
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
     // 上传前判断文件格式
    beforeUpload(file) {
      console.log(file.type);
       const type = file.type === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
      if (!type) {
        this.$message.error("请上传EXCEL格式的文件!");
      }
      return type;
    },
    //上传成功回调方法
    handlesuccess(res) {
      if (res.code == 200) {
        //刷新数据
        this.getdata();
        this.$message({
            type: "success",
            message: "操作成功！",
        });
      } else {
        this.$message({
            type: "info",
            message: res.message,
        });
      }
    },
    //编码保存页面
    submitmaterial_coder(){
        this.editForm.material_code=this.currentRow.material_code;
        this.editForm.warehousename=this.currentRow.warehousename;
        let u = localStorage.getItem("userdata");
        let user = JSON.parse(u)
        this.editForm.warehousing_personnel=user.name;
      this. material_codeVisible=false;
    },
     //关闭编码弹框
    closematerial_code(){
      this. material_codeVisible=false;
    },
    //获取编码选中的数据
      handleCurrentChange(val) {
      this.currentRow = val;
    },
    //弹出编码对应的选择框
    addmaterial_code(){
        this. material_codeVisible=true;
        this.$axios.get('/api/productionwarehouse/inventorydetails').then(res=>{
           this.material_codedata=res.data.data
      }).catch(err => {
          this.loading = false
          this.$message.error('菜单加载失败，请稍后再试！')
        })
    },
       //获取仓库数据
    getWarehouseData(){
      this.$axios.get('/api/warehouse/findAll').then(res=>{
        //console.log(res)
         this. warehouseData=res.data.data
      }).catch();
    },
    //获取编码数据
    getmaterial_codeData(){
        this.$axios.get("/api/production/production").then(res=>{
              console.log(res)
              this.material_codeData=res.data.data
        }).catch()
    
    },
    // 获取公司列表
    getdata(parameter) {
      this.loading = true
       this.$axios.get('/api/productionwarehouse/findAll/'+this.pageparm.currentPage+'/'+this.pageparm.pageSize)
        .then(res => {
          console.log(res)
          this.loading = false
          if (res.code==200) {
            this.$message({
              type: 'info',
              message: res.message
            })
          } else {
            this.listData = res.data.data.list
            // 分页赋值
            this.pageparm.total = res.data.data.total
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('菜单加载失败，请稍后再试！')
        })
     
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
       this.$axios.get('/api/productionwarehouse/findReceipt_No/'+this.formInline.receipt_No).then(res=>{
        this.pageparm.total=res.data.data.length
         this.listData=res.data.data
      }).catch()
    },
    //显示编辑界面
    handleEdit: function( row) {
      this.editFormVisible = true
       // console.log(user.name)
       this.getWarehouseData();
      this.getmaterial_codeData();
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.receipt_No=row.receipt_No
       this.editForm.material_code=row.material_code
       this.editForm.warehousename=row.warehousename
       this.editForm.receipt_quantity=row.receipt_quantity
       this.editForm.warehousing_personnel=row.warehousing_personnel
       this.editForm.warehousing_time=row.warehousing_time
      } else {
        this.title = '添加'
      this.editForm.receipt_No="WN-"+v4()
       this.editForm.material_code=""
       this.editForm.warehousename=""
       this.editForm.receipt_quantity=""
       this.editForm.warehousing_personnel=""
       this.editForm.warehousing_time=""
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editForm) {
      this.$refs[editForm].validate(valid => {
         if (!valid) {
          this.$message.error("请填写完表单！");
        }else{
       if(this.title==="添加"){
        this.$axios.post('/api/productionwarehouse/addStore',this.editForm).then(res=>{
                 this.editFormVisible = false;
                if (res.data.code==200) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '订单创建成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
      }).catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('新增订单，请稍后再试！')
            })
            this.$axios.put('/api/productionwarehouse/warehouse_num',this.editForm).then(res=>{

            }).catch()
      }else{
        this.$axios.put('/api/productionwarehouse/store',this.editForm).then(res=>{
            //关闭弹窗
                this.editFormVisible = false;
                if (res.data.code == 200) {
                    //刷新数据
                    this.getdata();
                    this.$message({
                        type: "success",
                        message: "修改成功！",
                    });
                } else {
                    this.$message({
                        type: "info",
                        message: res.data.message,
                    });
                }


        }).catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('修改订单，请稍后再试！')
            })
      }
      }
        })
    },
    // 删除公司
    deleteUser( row) {
      console.log(row.receipt_No)
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$axios.delete('/api/productionwarehouse/deleteStore/'+row.receipt_No)
            .then(res => {
              if (res.data.code==200) {
                this.$message({
                  type: 'success',
                  message: '订单已删除!'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('订单删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

 
 